package cn.edu.sjtu.se.peakfactory.actions;

import java.util.List;

import cn.edu.sjtu.se.lchang.socialnetwork.actions.BaseAction;
import cn.edu.sjtu.se.lchang.socialnetwork.actions.annotations.Action;
import cn.edu.sjtu.se.lchang.socialnetwork.actions.annotations.Action.MethodType;
import cn.edu.sjtu.se.lchang.socialnetwork.actions.annotations.Namespace;
import cn.edu.sjtu.se.lchang.socialnetwork.constants.RoleType;
import cn.edu.sjtu.se.lchang.socialnetwork.service.bean.UserVB;
import cn.edu.sjtu.se.peakfactory.service.bean.GameVB;
import cn.edu.sjtu.se.peakfactory.service.interfaces.GameService;

@Namespace(namespace = "game")
public class GameAction extends BaseAction {
	private static final long serialVersionUID = -4340590476930394030L;
	private GameVB game;
	private UserVB user;
	private Long gameId;
	private String name;
	private String description;
	private Integer type;
	private List<Long> tagIds;
	private String url;
	private String creator;
	private String normalPic;
	private String bigPic;
	private List<GameVB> games;
	private GameService gameService;

	@Action(type = MethodType.STRUTS_ACTION, params = { "gameId" }, roles = {
			RoleType.ROLE_ADMIN, RoleType.ROLE_USER })
	public String get_game() throws Exception {
		game = gameService.getGameById(gameId);
		if (game == null) {
			return setReturn(NOT_FOUND);
		}
		return setReturn(SUCCESS);
	}

	@Action(type = MethodType.AJAX_ACTION, params = { "gameId" }, roles = {
			RoleType.ROLE_ADMIN, RoleType.ROLE_USER }, root = "game")
	public String get_game_ajax() throws Exception {
		game = gameService.getGameById(gameId);
		return SUCCESS;
	}

	@Action(type = MethodType.STRUTS_ACTION, roles = { RoleType.ROLE_ADMIN })
	public String add_game_input() throws Exception {
		return setReturn(SUCCESS);
	}

	@Action(type = MethodType.STRUTS_ACTION, params = { "name", "description",
			"type", "tagIds", "url", "creator", "normalPic", "bigPic" }, roles = { RoleType.ROLE_ADMIN })
	public String add_game() throws Exception {
		user = getLogin();
		gameId = gameService.addGame(name, description, type, tagIds, url,
				creator, normalPic, bigPic);
		return setReturn(SUCCESS);
	}

	@Action(type = MethodType.STRUTS_ACTION, params = { "gameId" }, roles = { RoleType.ROLE_ADMIN })
	public String delete_game() throws Exception {
		gameService.deleteGame(gameId);
		return setReturn(SUCCESS);
	}

	@Action(type = MethodType.STRUTS_ACTION, params = { "gameId" })
	public String register_game() throws Exception {
		user = getLogin();
		gameService.registerGame(gameId, user.getId());
		return setReturn(SUCCESS);
	}

	@Action(type = MethodType.STRUTS_ACTION, params = { "gameId" })
	public String nullify_game() throws Exception {
		user = getLogin();
		gameService.nullifyGame(gameId, user.getId());
		return setReturn(SUCCESS);
	}

	@Action(type = MethodType.STRUTS_ACTION, params = { "gameId" })
	public String list_game() throws Exception {
		user = getLogin();
		games = gameService.listGame(user.getId());
		return setReturn(SUCCESS);
	}

	@Action(type = MethodType.AJAX_ACTION, params = { "gameId" }, root = "games")
	public String list_game_ajax() throws Exception {
		user = getLogin();
		games = gameService.listGame(user.getId());
		return SUCCESS;
	}
	
	@Action(type = MethodType.STRUTS_ACTION, params = { "name", "tagIds",
			"description" }, roles = { RoleType.ROLE_USER, RoleType.ROLE_ADMIN })
	public String find_game() throws Exception {
		games = gameService.findGame(name, tagIds, description);
		return setReturn(SUCCESS);
	}

	@Action(type = MethodType.AJAX_ACTION, roles = { RoleType.ROLE_USER,
			RoleType.ROLE_ADMIN })
	public String find_game_ajax() throws Exception {
		games = gameService.findGame(name, tagIds, description);
		return SUCCESS;
	}

	public GameVB getGame() {
		return game;
	}

	public void setGame(GameVB game) {
		this.game = game;
	}

	public UserVB getUser() {
		return user;
	}

	public void setUser(UserVB user) {
		this.user = user;
	}

	public Long getGameId() {
		return gameId;
	}

	public void setGameId(Long gameId) {
		this.gameId = gameId;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Integer getType() {
		return type;
	}

	public void setType(Integer type) {
		this.type = type;
	}

	public List<Long> getTagIds() {
		return tagIds;
	}

	public void setTagIds(List<Long> tagIds) {
		this.tagIds = tagIds;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getCreator() {
		return creator;
	}

	public void setCreator(String creator) {
		this.creator = creator;
	}

	public String getNormalPic() {
		return normalPic;
	}

	public void setNormalPic(String normalPic) {
		this.normalPic = normalPic;
	}

	public String getBigPic() {
		return bigPic;
	}

	public void setBigPic(String bigPic) {
		this.bigPic = bigPic;
	}
	
	public List<GameVB> getGames() {
		return games;
	}

	public void setGames(List<GameVB> games) {
		this.games = games;
	}
	
	public void setGameService(GameService gameService) {
		this.gameService = gameService;
	}
}
